package com.cmo.rzl.shine.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmo.rzl.core.dao.Db;
import com.cmo.rzl.core.jfinal.ext.kit.JsonKit;
import com.cmo.rzl.core.toolbox.Record;
import com.cmo.rzl.shine.model.Doctor;
import com.cmo.rzl.shine.model.Doctor2queuetype;
import com.cmo.rzl.shine.model.QueueType;
import com.cmo.rzl.shine.service.Doctor2queuetypeService;
import com.cmo.rzl.shine.service.DoctorService;
import com.cmo.rzl.shine.service.QueueTypeService;
import com.cmo.rzl.shine.service.impl.Doctor2queuetypeServiceImpl;
import com.cmo.rzl.shine.service.impl.DoctorServiceImpl;
import com.cmo.rzl.shine.service.impl.QueueTypeServiceImpl;
import com.cmo.rzl.system.controller.base.UrlPermissController;
import com.jfinal.kit.StrKit;

/**
 * Generated by Blade. 2017-08-03 10:50:24
 */
public class Doctor2queuetypeController extends UrlPermissController {
	private static String CODE = "doctor2queuetype";
	private static String PERFIX = "rlt_doctor2queue_type";
	private static String LIST_SOURCE = "Doctor2queuetype.list";
	private static String BASE_PATH = "/platform/doctor2queuetype/";

	Doctor2queuetypeService service = new Doctor2queuetypeServiceImpl();

	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "doctor2queuetype.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "doctor2queuetype_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Doctor2queuetype doctor2queuetype = service.findById(id);
		setAttr("model", JsonKit.toJson(doctor2queuetype));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "doctor2queuetype_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Doctor2queuetype doctor2queuetype = service.findById(id);
		setAttr("model", JsonKit.toJson(doctor2queuetype));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "doctor2queuetype_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Doctor2queuetype doctor2queuetype = mapping(PERFIX,
				Doctor2queuetype.class);
		boolean temp = service.save(doctor2queuetype);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Doctor2queuetype doctor2queuetype = mapping(PERFIX,
				Doctor2queuetype.class);

		boolean temp = service.update(doctor2queuetype);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}

	/*
	 * 医生编号 外键 必要 login_id Varchar(100) NOT NULL 对应doctor医生视图中login_id字段 队列类型编号
	 * 外键 必要 queue_type_source_id Varchar(100) NOT NULL
	 * 对应Queue_Type排队队列视图中source_id字段 排班标识 必要 onduty Varchar(7) NOT NULL 可选列
	 * 7位为每周上下午的标识， 0不出诊 1全天 2上午 3下午 例如：0131111 表示周一不出诊 周三下午出诊 其他时间全天
	 */
	public void save_rostering() {
		Map<String, Object> map = new HashMap<String, Object>();
		String schedulings_strs = getPara("schedulings");
		if (StrKit.isBlank(schedulings_strs)) {
			map.put("code", -1);
			map.put("msg", "请传值");
			renderJson(map);
			return;
		} else {
			JSONArray schedulingsList = JSONArray.parseArray(schedulings_strs);
			if (schedulingsList != null) {
				Doctor2queuetype rdq = null;
				for (Object json : schedulingsList) {
					JSONObject schedulings_json = (JSONObject) json;
					rdq = new Doctor2queuetype();
					DoctorService dsserver = new DoctorServiceImpl();
					Doctor doctor = dsserver.findByLogin_id(schedulings_json
							.getString("login_id"));
					int doctor_id = doctor.getDoctor_id();

					QueueTypeService qtservice = new QueueTypeServiceImpl();
					QueueType queuetype = qtservice
							.findBySource_id(schedulings_json
									.getString("queue_type_source_id"));
					int queuetypeid = queuetype.getQueue_type_id();

					List<Record> list = service.findInt(doctor_id, queuetypeid);
					if (!list.isEmpty()) {
						String sql = "DELETE FROM rlt_doctor2queue_type WHERE doctor_id="
								+ doctor_id
								+ " and queue_type_id="
								+ queuetypeid + "";
						Db.init().delete(sql, null);
						rdq.setDoctor_id(doctor_id);
						rdq.setQueue_type_id(queuetypeid);
						rdq.setOnduty(schedulings_json.getString("onduty"));
						boolean bool = false;
						try {
							bool = service.save(rdq);
							if (bool) {
								map.put("code", 200);
								map.put("msg", "成功");
								renderJson(map);
							} else {
								map.put("code", -1);
								map.put("msg", "失败");
								renderJson(map);
								return;
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					} else {
						rdq.setDoctor_id(doctor_id);
						rdq.setQueue_type_id(queuetypeid);
						rdq.setOnduty(schedulings_json.getString("onduty"));
						boolean bool = false;
						try {
							bool = service.save(rdq);
							if (bool) {
								map.put("code", 200);
								map.put("msg", "保存成功");
								renderJson(map);
							} else {
								map.put("code", -1);
								map.put("msg", "保存失败");
								renderJson(map);
								return;
							}
						} catch (Exception e) {
							map.put("code", -1);
							map.put("msg",
									"保存失败,传入的医生工号:"
											+ schedulings_json
													.getString("login_id")
											+ "或者队列工号:"
											+ schedulings_json
													.getString("queue_type_source_id")
											+ "已存在");
							renderJson(map);
							return;
						}

					}
				}
			}
		}
		renderJson(map);
	}
}
